package com.nelson.musicplayer.base

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.nelson.musicplayer.R

/**
 * @author nelson
 * @since 2020-07-28
 */
abstract class BaseDialog<out T : ViewDataBinding> : Dialog {
    private val mContext = context
    private val mViewDataBinding: T

    constructor(context: Context) : this(context, R.style.dialog)

    constructor(context: Context, themeResId: Int) : super(context, themeResId) {
        mViewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), getContentViewId(), null, false)
        setContentView(mViewDataBinding.root)
        init()
    }

    override fun onStart() {
        super.onStart()
        if (window != null) {
            val params = window!!.attributes
            if (getWidthId() != 0) {
                params.width = mContext.resources.getDimensionPixelOffset(getWidthId())
            }
            if (getHeightId() != 0) {
                params.height = mContext.resources.getDimensionPixelOffset(getHeightId())
            }
            window!!.attributes = params
        }
    }

    protected abstract fun getHeightId(): Int

    protected abstract fun getWidthId(): Int

    protected abstract fun init()

    protected abstract fun getContentViewId(): Int
}